package com.cloud.pole.shop.rabbit;

import com.alibaba.fastjson.JSON;
import com.cloud.pole.api.rabbit.DLXMessage;
import com.cloud.pole.common.constant.RebbitConstants;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.service.order.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: Frank Chen
 * @Date: 2020/3/6 11:44
 */
@Component
@Slf4j
public class RabbitReceiver {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    @RabbitListener(queues = RebbitConstants.REBBIT_SHOP_ORDER)
    public void handleOrder(String orderSn) {
        log.info("接受队列:"+ RebbitConstants.REBBIT_SHOP_ORDER);
        log.info("接受时间:"+ System.currentTimeMillis());
        log.info("接受消息:" + orderSn);
    }

    @RabbitHandler
    @RabbitListener(queues = RebbitConstants.REBBIT_SHOP_ORDER_REPEAT_TRADE)
    public void handleOrderDelay(String message) {
        //此时，才把消息发送到指定队列，而实现延迟功能
        DLXMessage dlxMessage = JSON.parseObject(message, DLXMessage.class);
        String orderSn = (String) dlxMessage.getContent();
        try{
            Boolean flag = orderService.synOrderDelay(orderSn);
            //处理成功
            if(flag){
                //处理订单
                log.info("处理订单超时成功：orderSn["+ orderSn +"] flag["+flag +"]");
            }
            //处理失败
            else{
                log.error("处理订单超时失败：orderSn["+ orderSn +"] flag["+flag +"]");
            }
        }
        catch (Exception e) {
            try {
                Thread.sleep(1000);
            }
            catch (Exception ex){}
                log.error("错误{}", e.getMessage(), e);
                e.printStackTrace();
        }
    }
    
    @RabbitHandler
    @RabbitListener(queues = RebbitConstants.REBBIT_SHOP_ORDER_PAY_SUCCEED)
    public void handleOrderPaySucceed(ShopOrder order) throws Exception{
        int returnBackStatus = 0;
        try {
            log.info("处理订单支付成功信息：{}", order);

        	
//处理成功
//            if(response.getStatus() == CommonConstants.STATUS_OK_CODE){
//                returnBackStatus = response.getData();
//                log.info("支付成功：" + api.toString());
//                log.info("支付成功：status["+response.getStatus()+"] data["+ response.getData() +"]");
//            }
//            //处理失败
//            else{
//                returnBackStatus = RebbitConstants.HANDLE_BALANCE_STATUS_ERROR;
//                log.error("支付失败：" + api.toString());
//                log.error("支付失败：status["+response.getStatus()+"] message["+response.getMessage()+"]");
//            }
        }
        catch (Exception e) {
            try { Thread.sleep(1000); }
            catch (Exception ex){}
            log.error("错误{}", e.getMessage(), e);
            e.printStackTrace();
        }
    }
}
